package com.fantastic.web.controller.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.result.ResultData;
import com.fantastic.common.utils.RandomUtil;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.vo.zjs.OrderVo;
import com.fantastic.service.zjs.AccountService;
import com.fantastic.web.util.AlipayKey;
import com.fantastic.web.util.AlipayPay;
import com.fantastic.web.util.AlipayPayUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author: rxx
 * @since: 2024/6/18 15:06
 * @description:
 */
@Controller
@RequestMapping("/alipay")
public class AlipayController {
    @Resource
    private AlipayClient alipayClient;
    @Resource
    private AlipayTradePagePayRequest pagePayRequest;
    @Resource
    private AlipayTradeAppPayRequest appPayRequest;
    @Resource
    private AlipayTradeWapPayRequest wapPayRequest;
    @Resource
    private AccountService accountService;

    /**
     * 网页支付
     *
     * @return
     */
    @RequestMapping("/webPay")
    public String alipay(OrderVo orders) {
        orders.setOrderId(RandomUtil.getCodeNumber(11));
        //向支付宝发送请求
        return AlipayPay.pageAlipayPay(alipayClient, pagePayRequest, orders);
    }

    /**
     * 充钱支付
     *
     * @param orders 订单对象
     * @return 返回支付宝特定的GSON字符串，交由前端进行解析（app端：如uniApp，解析方式查看具体的文档）
     */
    @RequestMapping("/wapPay")
    public void alipayWap(HttpServletResponse resp, OrderVo orders) {
        orders.setOrderId(RandomUtil.getCodeNumber(11));
        //向支付宝发送请求
        AlipayPay.wapAlipayPay(resp, alipayClient, wapPayRequest, orders);
    }


    /**
     * app支付对接请求地址
     *
     * @param orders 订单对象
     * @return 返回支付宝特定的GSON字符串，交由前端进行解析（app端：如uniApp，解析方式查看具体的文档）
     */
    @RequestMapping("/appPay")
    public void alipayApp(HttpServletResponse req, OrderVo orders) {
        orders.setOrderId(RandomUtil.getCodeNumber(11));
        //向支付宝发送请求
        AlipayPay.appAlipayPay(req, alipayClient, appPayRequest, orders);
    }

    /**
     * 沙箱异步响应给后端请求地址
     *
     * @param req
     */
    @RequestMapping("/notifyResult")
    public String notifyResult(HttpServletRequest req) {
        //解析响应过来的参数
        Map<String, String> alipayRespParam = AlipayPayUtil.alipayParamResolve(req);
        Integer accountId = Integer.valueOf(alipayRespParam.get("body"));
        BigDecimal totalAmount = new BigDecimal(alipayRespParam.get(AlipayKey.TOTAL_AMOUNT.getKeyValue()));
        BigDecimal money = AlipayPay.add(accountService.getAccount(
                accountId).getAccountStored(), totalAmount);
        //执行修改金额
        Account account = new Account();
        account.setAccountId(accountId);
        account.setAccountStored(money);
        accountService.updateAccount(account);
        //进行参数校验，检查支付状态。支付成功返回支付状态
        return AlipayPayUtil.alipayStatusCheck(alipayRespParam);
    }

    /**
     * 沙箱同步响应给后端的请求地址
     *
     * @param req
     */
    @RequestMapping("/returnResult")
    public void returnResult(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解析响应过来的参数
        Map<String, String> alipayRespParam = AlipayPayUtil.alipayParamResolve(req);
        //进行验签（不报错自动进入下一个方法）
        AlipayPayUtil.rsaCheckV1(alipayRespParam);
        //返回订单信息
//        return new ResultData(RespStatusInfo.ALIPAY_OK, AlipayPayUtil.getOrdersInfo(alipayRespParam));
        req.getRequestDispatcher("http://localhost:8081/#/pages/accountPay/accountPay").forward(req, resp);
    }

}
